﻿Public Class ConnectToMapForm
    Private AreaNam As String
    Private MapNam As String, MapNamFull As String
    Private SectorNam As String, SectorNamFull As String
    Private MapNum As Integer
    Public Success As Boolean = False
    Public Type As String = ""
    Private Sub loadconnecttomapform(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MapEditor.MapHeader = MapEditor.GetFileContents(Debugger.DirMapHeader) 'go ahead and re-update the mapheader incase it's changed
        Dim TypeNumFound As Boolean = True 'identifies whether the first mapvariable number has been found yet
        Dim TypeNum As Integer = 0 'this is the map variable number
        Dim BeginNam As Integer = 0 'This identifies where the first map is found
        Dim SectorSectionFound As Boolean 'this boolean is used to identify whether the recurser has reached teh sector section.
        Dim x As Long 'This is the variable number, this increases until it reaches the max map number
        CurMap.Items.Clear()
        While TypeNumFound = True
            TypeNum += 1
            If InStr(MapEditor.MapHeader, "MAP#" + LTrim(Str(TypeNum)) + "(") Then
                TypeNumFound = True
                x = InStr(MapEditor.MapHeader, "MAP#" + LTrim(Str(TypeNum)) + "(")
                BeginNam = 0
                While BeginNam = 0
                    x += 1
                    If Mid(MapEditor.MapHeader, x, 1) = "." Then
                        BeginNam = x
                        Exit While
                    End If
                End While
                x = BeginNam
                SectorSectionFound = False
                While SectorSectionFound = False
                    x += 1
                    If Mid(MapEditor.MapHeader, x, 1) = ")" Then
                        CurMap.Items.Add(LTrim(Str(TypeNum)) + ". " + Mid(MapEditor.MapHeader, BeginNam + 1, x - BeginNam - 1))
                        SectorSectionFound = True
                    End If
                End While
            Else
                TypeNumFound = False
            End If
        End While
        If MapEditor.CurArea.Text <> "Select Sector Area" Then
            CurMap.Text = CurMap.Items(0)
        End If
        If North.Enabled = False And South.Enabled = False And East.Enabled = False And West.Enabled = False And Up.Enabled = False And Down.Enabled = False Then
            AcceptBttn.Enabled = False
        Else
            AcceptBttn.Enabled = True
        End If
    End Sub

    Private Sub MapSelected(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CurMap.SelectedIndexChanged
       Dim TypeNumFound As Boolean = True 'identifies whether the first mapvariable number has been found yet
        Dim TypeNum As Integer = 0 'this is the map variable number
        Dim BeginNam As Integer = 0 'This identifies where the first map is found
        Dim SectorSectionFound As Boolean 'this boolean is used to identify whether the recurser has reached teh sector section.
        Dim x As Long 'This is the variable number, this increases until it reaches the max map number
        CurSector.Items.Clear()
        For x = 1 To Len(CurMap.Text) Step 1
            If Mid(CurMap.Text, x, 1) = "." Then
                MapNam = Replace(Mid(CurMap.Text, x + 2, Len(CurMap.Text)), ")", "")
                Exit For
            End If
        Next
        While TypeNumFound = True
            TypeNum += 1
            If InStr(MapEditor.MapHeader, "SECTOR#" + LTrim(Str(TypeNum)) + "(" + MapNam + ".") Then
                TypeNumFound = True
                x = InStr(MapEditor.MapHeader, "SECTOR#" + LTrim(Str(TypeNum)) + "(" + MapNam + ".")
                BeginNam = 0
                While BeginNam = 0
                    x += 1
                    If Mid(MapEditor.MapHeader, x, 1) = "." Then
                        BeginNam = x
                        Exit While
                    End If
                End While
                x = BeginNam
                SectorSectionFound = False
                While SectorSectionFound = False
                    x += 1
                    If Mid(MapEditor.MapHeader, x, 1) = ")" Then
                        CurSector.Items.Add(LTrim(Str(TypeNum)) + ". " + Mid(MapEditor.MapHeader, BeginNam + 1, x - BeginNam - 1))
                        SectorSectionFound = True
                    End If
                End While
            Else
                TypeNumFound = False
            End If
        End While
        If CurMap.Text <> "Select Sector Map" Then
            CurSector.Text = CurSector.Items(0)
        End If
    End Sub

    Private Sub CancelExit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CancelBttn.Click
        Up.Checked = True
        Me.Close()
    End Sub

    Private Sub AcceptAndClose(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcceptBttn.Click
        Dim tmp0 As Integer
        Dim TheMapString As String = "", TheSectorString As String = "", TheSectorStringLong As String = ""
        For tmp0 = 1 To Len(CurMap.Text) Step 1
            If Mid(CurMap.Text, tmp0, 1) = "." Then
                TheMapString = Mid(CurMap.Text, 1, tmp0 - 1)
            End If
        Next
        For tmp0 = 1 To Len(CurSector.Text) Step 1
            If Mid(CurSector.Text, tmp0, 1) = "." Then
                TheSectorString = Mid(CurSector.Text, 1, tmp0 - 1)
                TheSectorStringLong = LTrim(RTrim(MapEditor.SepByCapital(Mid(CurSector.Text, tmp0 + 1, Len(CurSector.Text)))))
            End If
        Next
        Success = True
        If North.Checked = True Then
            If MapEditor.ConnectLabel.Text = "Not Connected" Then
                MapEditor.ConnectLabel.Text = "North[" + TheMapString + "." + TheSectorString + "]"
            Else
                MapEditor.ConnectLabel.Text += ",North[" + TheMapString + "." + TheSectorString + "]"
            End If
            MapEditor.MapNorthLong(MapEditor.CurX, MapEditor.CurY) = TheSectorStringLong
            MapEditor.MapNorth(MapEditor.CurX, MapEditor.CurY) = "[" + TheMapString + "." + TheSectorString + "]"
            MapEditor.MapN(MapEditor.CurX, MapEditor.CurY) = True
            MapEditor.MapNOffMap(MapEditor.CurX, MapEditor.CurY) = True
        ElseIf South.Checked = True Then
            If MapEditor.ConnectLabel.Text = "Not Connected" Then
                MapEditor.ConnectLabel.Text = "South[" + TheMapString + "." + TheSectorString + "]"
            Else
                MapEditor.ConnectLabel.Text += ",South[" + TheMapString + "." + TheSectorString + "]"
            End If
            MapEditor.MapSouthLong(MapEditor.CurX, MapEditor.CurY) = TheSectorStringLong
            MapEditor.MapSouth(MapEditor.CurX, MapEditor.CurY) = "[" + TheMapString + "." + TheSectorString + "]"
            MapEditor.MapS(MapEditor.CurX, MapEditor.CurY) = True
            MapEditor.MapSOffMap(MapEditor.CurX, MapEditor.CurY) = True
        ElseIf East.Checked = True Then
            If MapEditor.ConnectLabel.Text = "Not Connected" Then
                MapEditor.ConnectLabel.Text = "East[" + TheMapString + "." + TheSectorString + "]"
            Else
                MapEditor.ConnectLabel.Text += ",East[" + TheMapString + "." + TheSectorString + "]"
            End If
            MapEditor.MapEastLong(MapEditor.CurX, MapEditor.CurY) = TheSectorStringLong
            MapEditor.MapEast(MapEditor.CurX, MapEditor.CurY) = "[" + TheMapString + "." + TheSectorString + "]"
            MapEditor.MapE(MapEditor.CurX, MapEditor.CurY) = True
            MapEditor.MapEOffMap(MapEditor.CurX, MapEditor.CurY) = True
        ElseIf West.Checked = True Then
            If MapEditor.ConnectLabel.Text = "Not Connected" Then
                MapEditor.ConnectLabel.Text = "West[" + TheMapString + "." + TheSectorString + "]"
            Else
                MapEditor.ConnectLabel.Text += ",West[" + TheMapString + "." + TheSectorString + "]"
            End If
            MapEditor.MapWestLong(MapEditor.CurX, MapEditor.CurY) = TheSectorStringLong
            MapEditor.MapWest(MapEditor.CurX, MapEditor.CurY) = "[" + TheMapString + "." + TheSectorString + "]"
            MapEditor.MapW(MapEditor.CurX, MapEditor.CurY) = True
            MapEditor.MapWOffMap(MapEditor.CurX, MapEditor.CurY) = True
        ElseIf Up.Checked = True Then
            If MapEditor.ConnectLabel.Text = "Not Connected" Then
                MapEditor.ConnectLabel.Text = "Up[" + TheMapString + "." + TheSectorString + "]"
            Else
                MapEditor.ConnectLabel.Text += ",Up[" + TheMapString + "." + TheSectorString + "]"
            End If
            MapEditor.MapUpLong(MapEditor.CurX, MapEditor.CurY) = TheSectorStringLong
            MapEditor.MapUp(MapEditor.CurX, MapEditor.CurY) = "[" + TheMapString + "." + TheSectorString + "]"
            MapEditor.MapU(MapEditor.CurX, MapEditor.CurY) = True
        ElseIf Down.Checked = True Then
            If MapEditor.ConnectLabel.Text = "Not Connected" Then
                MapEditor.ConnectLabel.Text = "Down[" + TheMapString + "." + TheSectorString + "]"
            Else
                MapEditor.ConnectLabel.Text += ",Down[" + TheMapString + "." + TheSectorString + "]"
            End If
            MapEditor.MapDownLong(MapEditor.CurX, MapEditor.CurY) = TheSectorStringLong
            MapEditor.MapDown(MapEditor.CurX, MapEditor.CurY) = "[" + TheMapString + "." + TheSectorString + "]"
            MapEditor.MapD(MapEditor.CurX, MapEditor.CurY) = True
        Else
            Success = False
        End If
        If Success = True Then
            Dim xish As Integer 'makes the code smaller, easier to read
            Dim yish As Integer 'also makes it smaller
            Dim Columns As Integer = 15, Rows As Integer = 15, TheRoomWidth As Integer = 32
            Dim TheRoomHeight As Integer = 32, RowSpace As Integer = 5, ColumnsSpace As Integer = 5
            Dim xishPLUS As Integer = Val(Columns) * Val(TheRoomWidth) + Val(Columns) * Val(RowSpace)
            Dim yishPLUS As Integer = Val(Rows) * Val(TheRoomHeight) + Val(Rows) * Val(ColumnsSpace)
            Dim ThePen As New Pen(Color.LightGray, 1)
            ThePen.DashStyle = Drawing2D.DashStyle.Dot
            xish = Val(TheRoomWidth) * MapEditor.CurX + Val(ColumnsSpace) * MapEditor.CurX + 10
            yish = Val(TheRoomHeight) * MapEditor.CurY + Val(RowSpace) * MapEditor.CurY + 10
            xishPLUS = Val(TheRoomWidth) * MapEditor.CurX + Val(ColumnsSpace) * MapEditor.CurX + 10 + Val(TheRoomWidth)
            yishPLUS = Val(TheRoomHeight) * MapEditor.CurY + Val(RowSpace) * MapEditor.CurY + 10 + Val(TheRoomHeight)
            MapEditor.canvas.DrawLine(ThePen, xish - 2, yish - 2, xishPLUS + 2, yish - 2)
            MapEditor.canvas.DrawLine(ThePen, xishPLUS + 2, yish - 2, xishPLUS + 2, yishPLUS + 2)
            MapEditor.canvas.DrawLine(ThePen, xishPLUS + 2, yishPLUS + 2, xish - 2, yishPLUS + 2)
            MapEditor.canvas.DrawLine(ThePen, xish - 2, yishPLUS + 2, xish - 2, yish - 2)
            MapEditor.repaint()
        End If
        Up.Checked = True
        Me.Close()
    End Sub
End Class